package com.yanqu.road.server.manger.activity.chefduel;

import com.yanqu.road.dao.impl.player.UserChefDuelDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.chefduel.ChefDuelData;
import com.yanqu.road.entity.activity.chefduel.process3.ChefDuel3OpenItem;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.activity.chefDuel.eChefDuelActivityType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.chefduel.LogChefDuelPromotion;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.task.args.RelationActivityUnionRiseRankArgs;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.chefduel.ChefDuelProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.chefduel.ChefDuelModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.RelationActivityMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

public class ChefDuel3 {

    private ActivityInfo activityInfo;

    public boolean init() {
        return reload();
    }

    public boolean reload() {
        chefDuelDataMap = new UserChefDuelDaoImpl().getUserChefDuelMap(activityInfo.getActivityId());
        return true;
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public void setActivityInfo(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    public ChefDuel3(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    private Logger logger = LogManager.getLogger(ChefDuel3.class.getName());

    private final Map<Long,Object> lockMap = new ConcurrentHashMap<>();

    //用户id 用户数据
    private Map<Long, ChefDuelData> chefDuelDataMap = new ConcurrentHashMap<>();

    private List<ChefDuelData> needAddChefDuelList = new ArrayList<>();

    private List<ChefDuelData> needUpdateChefDuelList = new ArrayList<>();

    //正在发菜场景的人
    private Map<Long,List<Long>> sceneUserMap = new ConcurrentHashMap<>();

    public Object getLock(long userId){
        synchronized (lockMap){
            if(!lockMap.containsKey(userId)){
                lockMap.put(userId,new Object());
            }
            return lockMap.get(userId);
        }
    }

    public boolean isSceneUser(long userId,long openBanquetUserId){
        for(Long inUserId : sceneUserMap.get(openBanquetUserId)){
            if(inUserId == userId){
                return true;
            }
        }
        return false;
    }

    public void addPromotionUserList(List<ChefDuelProto.CrossChefDuelWindUpCookerTempMsg> userIdList, Map<Long, ChefDuelData> windUpUserChefDuel2Map) {
        windUpChefDuel(userIdList);
        ChefDuelProto.CrossChefDuel3CookerJoinSyncMsg.Builder reqMsg = ChefDuelProto.CrossChefDuel3CookerJoinSyncMsg.newBuilder();
        reqMsg.setActivityId(activityInfo.getActivityId());
        for(ChefDuelProto.CrossChefDuelWindUpCookerTempMsg tempMsg : userIdList){
            ChefDuelProto.CrossChefDuelWindUpCookerTempMsg.Builder userMsg = ChefDuelProto.CrossChefDuelWindUpCookerTempMsg.newBuilder();
            userMsg.setUserId(tempMsg.getUserId());
            userMsg.setScore(tempMsg.getScore());//不知道哪个前面的活动晋级的
            userMsg.setRank(tempMsg.getRank());
//            userMsg.setCampId(tempMsg.getCampId());
            userMsg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(UserMgr.getUserInfo(tempMsg.getUserId()), GameServer.getInstance().getServerId())));
            reqMsg.addUserInfoList(userMsg);
        }
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_CHEF_DUEL3_COOKER_JOIN, reqMsg);
        GamePlayerMgr.sendPacket(0, pbMsg);
    }

    //晋级玩家数据生成
    public void windUpChefDuel(List<ChefDuelProto.CrossChefDuelWindUpCookerTempMsg> userIdList){
        for(ChefDuelProto.CrossChefDuelWindUpCookerTempMsg tempMsg : userIdList){
            if(!chefDuelDataMap.containsKey(tempMsg.getUserId())){
                ChefDuelData value = initChefDuel3(tempMsg.getUserId());
                chefDuelDataMap.put(tempMsg.getUserId(), value);
            }
            ChefDuelData chefDuelData = chefDuelDataMap.get(tempMsg.getUserId());
            chefDuelData.setPreRank(tempMsg.getRank());
            chefDuelData.setAble(true);
            //根据排名给阵营
            if(tempMsg.getRank() <= 0){
                chefDuelData.setCampId(new Random().nextInt(2) + 1);
            }else {
                int i = tempMsg.getRank() % 4;
                if(i < 2){
                    chefDuelData.setCampId(1);
                }else {
                    chefDuelData.setCampId(2);
                }
            }
            synchronized (needUpdateChefDuelList){
                if(!needUpdateChefDuelList.contains(chefDuelData)){
                    needUpdateChefDuelList.add(chefDuelData);
                }
            }
            GamePlayer player = GamePlayerMgr.getOnlinePlayer(tempMsg.getUserId());
            if(player != null){
                player.getModule(ChefDuelModule.class).syncSelfBanquetTable(activityInfo.getActivityId());
            }
            AutoLogMgr.add(new LogChefDuelPromotion(activityInfo.getActivityId(),
                    chefDuelData.getUserId(),
                    3,
                    tempMsg.getScore(),
                    tempMsg.getRank(),
                    chefDuelData.getCampId()));
        }
    }

    public int worship(long userId){
        Object locker = getLock(userId);
        synchronized (locker){
            ChefDuelData chefDuelData = chefDuelDataMap.get(userId);
            if(chefDuelData.isWorship()){
                return GameErrorCode.E_CHEF_DUEL_FOOD_IS_WORSHIP;
            }
            GamePlayer gamePlayer = GamePlayerMgr.getOnlinePlayer(userId);
            if(gamePlayer != null){
                chefDuelData.setWorship(true);
                synchronized (needUpdateChefDuelList){
                    if(!needUpdateChefDuelList.contains(chefDuelData)){
                        needUpdateChefDuelList.add(chefDuelData);
                    }
                }
                gamePlayer.getModule(CurrencyModule.class).addCurrency(ChefDuelActivityMgr.getWorship3Reward(activityInfo.getActivityId()), eLogMoneyType.ChefDuel,eLogMoneyType.CrossChefDuelActivityWorship);
            }
        }
        return 0;
    }

    public void addSceneUser(long userId,long openBanquetUserId){
        if(!sceneUserMap.containsKey(openBanquetUserId)){
            sceneUserMap.put(openBanquetUserId,new ArrayList<>());
        }
        for(List<Long> userIdList : sceneUserMap.values()){
            userIdList.remove(userId);
        }
        sceneUserMap.get(openBanquetUserId).add(userId);
    }

    public void removeSceneUser(long userId,long openBanquetUserId){
        if(sceneUserMap.containsKey(openBanquetUserId)){
            sceneUserMap.get(openBanquetUserId).remove(userId);
        }
    }

    public List<Long> getSceneUser(long openBanquetUserId){
        return new ArrayList<>(sceneUserMap.get(openBanquetUserId));
    }

    public String ChefDuel3joinerBackMsg(long userId,int banquetItemId){
        Object locker = getLock(userId);
        synchronized (locker){
            String reward = ChefDuelActivityMgr.getChefDuel3OpenJoinerReward(activityInfo.getActivityId(),banquetItemId);
            GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);

            OpenGoodsBagResult openReward = GoodsBagMgr.getRandomGoods(StringUtils.stringToIntegerList(reward,"=").get(0));
            player.getModule(CurrencyModule.class).addCurrency(openReward.getReward(),eLogMoneyType.ChefDuel,eLogMoneyType.CrossChefDuel3JoinBanquetReward);
            return PropertyHelper.parsePropertyToString(openReward.getReward());
        }
    }

    public void chefDuel3RobotBackMsg(long userId,long integral){
        Object locker = getLock(userId);
        synchronized (locker){
            ChefDuelData chefDuelData = chefDuelDataMap.get(userId);
            GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
            if(player != null){
                int integralId = ChefDuelActivityMgr.getChefDuel3IntegralItemId(activityInfo.getActivityId());
                String reward = integralId + "=" + integral;
                player.getModule(CurrencyModule.class).addCurrency(reward,eLogMoneyType.ChefDuel,eLogMoneyType.CrossChefDuel3JoinBanquetOwnerReward);
                player.getModule(ChefDuelModule.class).syncSelfBanquetTable(activityInfo.getActivityId());
            }else{
                chefDuelData.setIntegral(chefDuelData.getIntegral() + integral);
            }
            chefDuelData.setAllIntegral(chefDuelData.getAllIntegral() + integral);
            chefDuelData.setRankTime(System.currentTimeMillis());
            synchronized (needUpdateChefDuelList){
                if(!needUpdateChefDuelList.contains(chefDuelData)){
                    needUpdateChefDuelList.add(chefDuelData);
                }
            }
            UserInfo userInfo = UserMgr.getUserInfo(userId);
            RelationActivityMgr.conditionNotify(activityInfo.getActivityId(),userId, eGamePlayerEventType.ChefDuel3IntegralUserCrossRank.getValue(),
                    new RelationActivityUnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_NORMAL, BigInteger.valueOf(integral),userInfo.getUnionUid(), activityInfo.getActivityId()));

        }
    }

    public void chefDuel3OpenerBackMsg(long userId,int banquetItemId){
        Object locker = getLock(userId);
        synchronized (locker){
            ChefDuelData chefDuelData = chefDuelDataMap.get(userId);
            ChefDuel3OpenItem chefDuel3OpenItem = ChefDuelActivityMgr.getChefDuel3OpenItem(activityInfo.getActivityId(),banquetItemId);
            GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
            chefDuelData.setAllIntegral(chefDuelData.getAllIntegral() + chefDuel3OpenItem.getIntegral());
            chefDuelData.setRankTime(System.currentTimeMillis());
            if(player != null){
                int integralId = ChefDuelActivityMgr.getChefDuel3IntegralItemId(activityInfo.getActivityId());
                String reward = integralId + "=" + chefDuel3OpenItem.getIntegral();
//				player.getModule(CurrencyModule.class).addCurrency(reward,eLogMoneyType.ChefDuel,eLogMoneyType.CrossChefDuel3JoinBanquetOwnerReward);
                chefDuelData.setIntegral(chefDuelData.getIntegral() + chefDuel3OpenItem.getIntegral());
                lockMap.put(0 - userId, System.currentTimeMillis());
            }else{
                chefDuelData.setIntegral(chefDuelData.getIntegral() + chefDuel3OpenItem.getIntegral());
            }
            synchronized (needUpdateChefDuelList){
                if(!needUpdateChefDuelList.contains(chefDuelData)){
                    needUpdateChefDuelList.add(chefDuelData);
                }
            }
            UserInfo userInfo = UserMgr.getUserInfo(userId);
            RelationActivityMgr.conditionNotify(activityInfo.getActivityId(),userId, eGamePlayerEventType.ChefDuel3IntegralUserCrossRank.getValue(),
                    new RelationActivityUnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_NORMAL, BigInteger.valueOf(chefDuel3OpenItem.getIntegral()),userInfo.getUnionUid(), activityInfo.getActivityId()));

        }
    }

    //参加宴会
    public int sendJoinBanquetMsg(long userId,long homeUserId,int banquetId){
        //修复userInfo没有发送到跨服保存
        ChefDuelProto.CrossChefDuel3OpenBanquetReqMsg.Builder reqMsgBug = ChefDuelProto.CrossChefDuel3OpenBanquetReqMsg.newBuilder();
        reqMsgBug.setActivityId(activityInfo.getActivityId());
        reqMsgBug.setUserId(userId);
        reqMsgBug.setItemId(-1);
        UserInfo userInfo = UserMgr.getUserInfo(userId);
        reqMsgBug.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(userInfo, GameServer.getInstance().getServerId())));
        YanQuMessage pbMsgBug = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_CHEF_DUEL3_OPEN_BANQUET, reqMsgBug);
        GamePlayerMgr.sendPacket(userId, pbMsgBug);

        ChefDuelProto.ChefDuel3JoinBanquetReqMsg.Builder reqMsg = ChefDuelProto.ChefDuel3JoinBanquetReqMsg.newBuilder();
        reqMsg.setActivityId(activityInfo.getActivityId());
        reqMsg.setBanquetId(banquetId);
        reqMsg.setUserId(homeUserId);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_CHEF_DUEL3_JOIN_BANQUET, reqMsg);
        GamePlayerMgr.sendPacket(userId, pbMsg);
        return 0;
    }

    public int sendOpenTable(long userId,int itemId){
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if(player != null){
            ChefDuelProto.CrossChefDuel3OpenBanquetReqMsg.Builder reqMsg = ChefDuelProto.CrossChefDuel3OpenBanquetReqMsg.newBuilder();
            reqMsg.setActivityId(activityInfo.getActivityId());
            reqMsg.setUserId(userId);
            reqMsg.setItemId(itemId);
            reqMsg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId())));
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_CHEF_DUEL3_OPEN_BANQUET, reqMsg);
            GamePlayerMgr.sendPacket(userId, pbMsg);
        }
        return 0;
    }

    public void syncChefDuel3Table(ChefDuelProto.CrossChefDuel3TableSyncMsg msg){
        List<GamePlayer> userList = GamePlayerMgr.getAllOnlinePlayer();
        for(GamePlayer player : userList){
            //解锁了同步
            if(chefDuelDataMap.containsKey(player.getUserId())){
                ChefDuelProto.ChefDuel3CookerRespMsg.Builder syncMsg = ChefDuelProto.ChefDuel3CookerRespMsg.newBuilder();
                syncMsg.setIsSync(true);
                syncMsg.setRet(0);
                syncMsg.addAllCampScore(msg.getCampScoreList());
                for(ChefDuelProto.ChefDuel3CookerCrossTempMsg tempMsg : msg.getTableListList()){
                    ChefDuelProto.ChefDuel3CookerTempMsg.Builder tableMsg = ChefDuelProto.ChefDuel3CookerTempMsg.newBuilder();
                    tableMsg.setRank(tempMsg.getRank());
                    tableMsg.setUserId(tempMsg.getUserId());
                    tableMsg.setOpenItemId(tempMsg.getOpenItemId());
                    tableMsg.setBanquetId(tempMsg.getBanquetId());
                    tableMsg.setOpenTime(tempMsg.getOpenTime());
                    tableMsg.setUserNum(tempMsg.getUserNum());
                    tableMsg.setBanquetCount(tempMsg.getBanquetCount());
                    tableMsg.setUserInfo(tempMsg.getUserInfo());
                    if(tempMsg.getBanquetId() != 0 && !tempMsg.getUserLitList().contains(player.getUserId())){
                        tableMsg.setCanEat(true);
                    }else{
                        tableMsg.setCanEat(false);
                    }
                    tableMsg.setCampId(tempMsg.getCampId());
                    syncMsg.addCookerList(tableMsg);
                }
                player.sendPacket(Protocol.U_CHEF_DUEL3_GET_TABLE_USER, syncMsg);
            }
        }
    }


    public void syncChefDuel3BanquetUserData(ChefDuelProto.ChefDuel3UserRewardSyncMsg msg){
        List<GamePlayer> userList = GamePlayerMgr.getAllOnlinePlayer();
        for(GamePlayer player : userList){
            ChefDuelProto.ChefDuel3UserRewardSyncMsg.Builder syncMsg = ChefDuelProto.ChefDuel3UserRewardSyncMsg.newBuilder(msg);
            player.sendPacket(Protocol.U_CHEF_DUEL3_BANQUET_USER_DATA_SYNC, syncMsg);
        }
    }

    public void syncChefDuel3BanquetOpenNotice(ChefDuelProto.ChefDuel3BanquetOpenSyncMsg msg){
        List<GamePlayer> userList = GamePlayerMgr.getAllOnlinePlayer();
        for(GamePlayer player : userList){
            ChefDuelProto.ChefDuel3BanquetOpenSyncMsg.Builder syncMsg = ChefDuelProto.ChefDuel3BanquetOpenSyncMsg.newBuilder(msg);
            player.sendPacket(Protocol.U_CHEF_DUEL3_OPEN_BANQUET_NOTICE_SYNC, syncMsg);
        }
    }

    public int sendCookerMsg(long userId,long homeUserId){
        //请求的时候要加上是不是进去场景的
        ChefDuelProto.ChefDuel3CookerReqMsg.Builder reqMsg = ChefDuelProto.ChefDuel3CookerReqMsg.newBuilder();
        reqMsg.setActivityId(activityInfo.getActivityId());
        reqMsg.setUserId(homeUserId);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_CHEF_DUEL3_COOKER_REQ, reqMsg);
        GamePlayerMgr.sendPacket(userId, pbMsg);
        return 0;
    }

    public ChefDuelData getUserChefDuelData(GamePlayer player){
        if(!chefDuelDataMap.containsKey(player.getUserId())){
//			clearIntegral(player,ChefDuelActivityMgr.getChefDuel3IntegralItemId(activityInfo.getActivityId()));
            chefDuelDataMap.put(player.getUserId(),initChefDuel3(player.getUserId()));
        }else{
            reAddIntegral(player,ChefDuelActivityMgr.getChefDuel3IntegralItemId(activityInfo.getActivityId()));
        }
        return chefDuelDataMap.get(player.getUserId());
    }

    public void setUserChefDuel3Able(long userId){
        if(chefDuelDataMap.containsKey(userId)){
            chefDuelDataMap.get(userId).setAble(true);
        }
    }

    public boolean isUserChefDuel3Able(long userId){
        if(chefDuelDataMap.containsKey(userId)){
            return chefDuelDataMap.get(userId).isAble();
        }
        return false;
    }


    public void reAddIntegral(GamePlayer player,int itemId){
        Object locker = getLock(player.getUserId());
        synchronized (locker){
            int integralItemId = ChefDuelActivityMgr.getChefDuel3IntegralItemId(activityInfo.getActivityId());
            if(itemId == integralItemId){
                ChefDuelData chefDuelData = chefDuelDataMap.get(player.getUserId());
                if(chefDuelData.getIntegral() > 0){
                    player.getModule(CurrencyModule.class).addCurrency(integralItemId + "=" + chefDuelData.getIntegral(), eLogMoneyType.ChefDuel,eLogMoneyType.CrossChefDuel3ActivityLogin);
                    chefDuelData.setIntegral(0);
                    synchronized (needUpdateChefDuelList){
                        if(!needUpdateChefDuelList.contains(chefDuelData)){
                            needUpdateChefDuelList.add(chefDuelData);
                        }
                    }
                }
            }
        }
    }

    public void clearIntegral(GamePlayer player,int itemId){
        Object locker = getLock(player.getUserId());
        synchronized (locker){
            int integralItemId = ChefDuelActivityMgr.getChefDuel3IntegralItemId(activityInfo.getActivityId());
            if(itemId == integralItemId){
                UserBagItem userBagItem = player.getModule(BagModule.class).getUserBagItem(integralItemId);
                if(userBagItem != null){
                    player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(integralItemId + "=" + userBagItem.getNum()), eLogMoneyType.ChefDuel,eLogMoneyType.CrossChefDuel3ActivityNewAc);
                }
            }
        }
    }

    private ChefDuelData initChefDuel3(long userId){
        ChefDuelData temp = new ChefDuelData();
        temp.setActivityId(activityInfo.getActivityId());
        temp.setUserId(userId);
        temp.setNo(activityInfo.getChildType());
        temp.setAble(false);
        temp.setIntegral(0);
        temp.setAllIntegral(0);
        temp.setRankTime(System.currentTimeMillis());
        temp.setHp(0);
        temp.setHpTime(System.currentTimeMillis());
        temp.setFinishFoodId(0);
        temp.setTableFoodData("{}");
        temp.setFinishFoodCount(0);
        temp.setFinishFoodData("{}");
        temp.setDishId(0);
        temp.setProcess(0);
        temp.setDishCount(0);
        synchronized (needAddChefDuelList){
            if(!needAddChefDuelList.contains(temp)){
                needAddChefDuelList.add(temp);
            }
        }
        return temp;
    }

    public boolean stop() {
        return false;
    }

    public boolean save() {
		/*if(System.currentTimeMillis() < 1629640800000L && !lockMap.containsKey(-880L)){
			String addInfo = "201002330003646=128000;";
			List<String> StringList = StringUtils.stringToStringList(addInfo,";");
			for(String infoStr : StringList){
				List<Long> listLong = StringUtils.stringToLongList(infoStr,"=");
				long userId = listLong.get(0);
				long integral = listLong.get(1);
				ChefDuelData chefDuelData = chefDuelDataMap.get(userId);
				if(chefDuelData != null){
					synchronized (getLock(userId)){
						RelationActivityMgr.conditionNotify(activityInfo.getActivityId(),userId, eGamePlayerEventType.ChefDuel3IntegralUserCrossRank.getValue(),
								new RelationActivityUnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_NORMAL, BigInteger.valueOf(integral),"", activityInfo.getActivityId()));
						chefDuelData.setAllIntegral(chefDuelData.getAllIntegral()+integral);
						logger.error(userId+"增加小当家阶段3"+getActivityInfo().getActivityId()+"积分"+integral);
						synchronized (needUpdateChefDuelList){
							if(!needUpdateChefDuelList.contains(chefDuelData)){
								needUpdateChefDuelList.add(chefDuelData);
							}
						}
					}
				}
			}
			lockMap.put(-880L,1L);
		}*/
        List<ChefDuelData> tempAddList;
        synchronized (needAddChefDuelList) {
            tempAddList = new ArrayList<>(needAddChefDuelList);
            needAddChefDuelList.clear();
        }
        if (tempAddList.size() > 0) {
            new UserChefDuelDaoImpl().addUserCooBoyList(tempAddList);
        }
        //保存记录
        List<ChefDuelData> tempUpdataList;
        synchronized (needUpdateChefDuelList) {
            tempUpdataList = new ArrayList<>(needUpdateChefDuelList);
            needUpdateChefDuelList.clear();
        }
        if (tempUpdataList.size() > 0) {
            new UserChefDuelDaoImpl().updateUserCooBoyList(tempUpdataList);
        }
        for (Long userId : new ArrayList<>(lockMap.keySet())) {
            if (userId < 0) {
                Object o = lockMap.get(userId);
                long time = (long) o;
                long l = System.currentTimeMillis() - time;
                if (l < 30000) {
                    GamePlayer player = GamePlayerMgr.getOnlinePlayer(0 - userId);
                    if (player != null) {
                        player.getModule(ChefDuelModule.class).syncSelfBanquetTable(activityInfo.getActivityId());
                        reAddIntegral(player,ChefDuelActivityMgr.getChefDuel3IntegralItemId(activityInfo.getActivityId()));
                    }
                }

            }

        }
        return true;
    }

    public void openBanquetAddCondition(long userId) {
        RelationActivityMgr.conditionNotify(activityInfo.getActivityId(),userId, eGamePlayerEventType.ChefDuel3BanquetOpenTimes.getValue(),1);
    }

    //加满参加阶段3
    private void test3(){
        List<ActivityInfo> activityInfoList = ChefDuelActivityMgr.getOpenActivityInfoList(eActivityType.CookingBoyActivity.getValue(), eChefDuelActivityType.Phase3.getChildType());
        for(ActivityInfo activityInfo : activityInfoList){
            ChefDuelProto.CrossChefDuel3CookerJoinSyncMsg.Builder reqMsg = ChefDuelProto.CrossChefDuel3CookerJoinSyncMsg.newBuilder();
            reqMsg.setActivityId(activityInfo.getActivityId());
            for(UserInfo userInfo : UserMgr.getUserInfoList()){
                if(!chefDuelDataMap.containsKey(userInfo.getUserId())){
                    initChefDuel3(userInfo.getUserId());
                }
                setUserChefDuel3Able(userInfo.getUserId());
                ChefDuelProto.CrossChefDuelWindUpCookerTempMsg.Builder userMsg = ChefDuelProto.CrossChefDuelWindUpCookerTempMsg.newBuilder();
                userMsg.setUserId(userInfo.getUserId());
                userMsg.setScore(0);//不知道哪个前面的活动晋级的
                userMsg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(userInfo, GameServer.getInstance().getServerId())));
                reqMsg.addUserInfoList(userMsg);
            }
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_CHEF_DUEL3_COOKER_JOIN, reqMsg);
            GamePlayerMgr.sendPacket(0, pbMsg);
        }
    }

    public void supportCamp(long playerId, int campId) {
        ChefDuelData chefDuelData = chefDuelDataMap.get(playerId);
        if(chefDuelData != null){
            chefDuelData.setSupportCampId(campId);
            synchronized (needUpdateChefDuelList){
                if(!needUpdateChefDuelList.contains(chefDuelData)){
                    needUpdateChefDuelList.add(chefDuelData);
                }
            }
        }
        YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_CHEF_DUEL3_SUPPORT_CAMP, ChefDuelProto.ChefDuel2CampSelectRespMsg.newBuilder().setRet(0).setCampId(campId));
        GamePlayerMgr.sendPacket(playerId, message);
    }

    private long lastUploadTime = System.currentTimeMillis();

    public void checkUpUserScoreUpload() {
        ChefDuelProto.CrossChefDuel2ScoreUploadMsg.Builder msg = ChefDuelProto.CrossChefDuel2ScoreUploadMsg.newBuilder();
        msg.setActivityId(activityInfo.getActivityId());
        long lastUploadTime = System.currentTimeMillis();
        for (Map.Entry<Long, ChefDuelData> entry : chefDuelDataMap.entrySet()) {
            if(entry.getValue().getRankTime() >= this.lastUploadTime){
                ChefDuelProto.CrossChefDuel2ScoreTemp.Builder temp = ChefDuelProto.CrossChefDuel2ScoreTemp.newBuilder();
                temp.setUserId(entry.getKey());
                temp.setScore(entry.getValue().getAllIntegral());
                temp.setTime(entry.getValue().getRankTime());
                msg.addScore(temp);
            }
        }
        this.lastUploadTime = lastUploadTime;

        if(msg.getScoreCount() > 0){
            GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_CROSS_CHEF_DUEL3_SCORE_UPLOAD, msg));
        }
    }
}
